package org.edu.model;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.GenericGenerator;
/**
 * <h2>角色表</h2>
 * <p>该实例讲解了 <br>角色与权限 的 many-to-many 关系  <br> 用户 （many）与 主角色（one） 的管理 <br>
 * 并写明了配置要点
 * </p>
 * @author 谢圣强 
 * @version 1.1 
 * @see User
 * @see Purview
 *
 */
@Entity
@Table(name = "T_role")
public class Role {
	private String id;
	private String name;
	private Set<Purview> purview;

	private Set<User> userid;

	@Id
	@GenericGenerator(name = "idGenerator", strategy = "uuid")
	@GeneratedValue(generator = "idGenerator")
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Column(name = "t_name", nullable = true)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@ManyToMany(targetEntity = org.edu.model.Purview.class, cascade = {
			CascadeType.MERGE, CascadeType.PERSIST })
	@Fetch(FetchMode.JOIN)
	@JoinTable(name = "T_Role_Purview", joinColumns = { @JoinColumn(name = "role_id") }, inverseJoinColumns = { @JoinColumn(name = "purview_id") })
	public Set<Purview> getPurview() {
		return purview;
	}

	public void setPurview(Set<Purview> purview) {
		this.purview = purview;
	}

	@OneToMany(targetEntity = org.edu.model.User.class, cascade = {
			CascadeType.REFRESH, CascadeType.PERSIST, CascadeType.REMOVE,
			CascadeType.MERGE }, fetch = FetchType.LAZY)
	@Fetch(FetchMode.JOIN)
	@JoinColumn(name = "roleid", updatable = false)
	public Set<User> getUserid() {
		return userid;
	}

	public void setUserid(Set<User> userid) {
		this.userid = userid;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return this.name;
	}
	
	

}
